package com.nest.WorkNest.act;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.FragmentViewPagerAdapter;
import com.nest.WorkNest.fragment.AlreadyReceive;
import com.nest.WorkNest.fragment.WaitPayment;
import com.nest.WorkNest.fragment.WaitReceive;

import java.util.ArrayList;

/**
 *
 *
 */
public class BulkOrderAct extends FragmentActivity
        implements OnClickListener {
    protected TextView actionbar_side_tab_name;
    protected LinearLayout ll_iv_actionbar_tab_side_left;
    private ViewPager pager;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private View slide_rod;
    private TextView tv_waitReceive, tv_alreadyReceive, tv_waitPayment;
    private WindowManager manager;
    private int width, tabLength, currIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_mybulkorder);
        initHead();
        initView();
        initViewPage();

    }

    public void initHead() {
        ll_iv_actionbar_tab_side_left = (LinearLayout) findViewById(
                R.id.ll_iv_actionbar_tab_side_left);
        actionbar_side_tab_name = (TextView) findViewById(R.id.actionbar_tab_side_name);
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        ll_iv_actionbar_tab_side_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("我的大货单");
    }

    private void initView() { /* 初始化其他控件*/
        pager = (ViewPager) findViewById(R.id.match_content);
        slide_rod = findViewById(R.id.slide_rod);
        tv_waitReceive = (TextView) findViewById(R.id.wait_receive);
        tv_alreadyReceive = (TextView) findViewById(R.id.already_receive);
        tv_waitPayment = (TextView) findViewById(R.id.wait_payment);
        tv_waitReceive.setOnClickListener(this);
        tv_alreadyReceive.setOnClickListener(this);
        tv_waitPayment.setOnClickListener(this);

        // 获取屏幕宽度，初始化滑块的宽度
        DisplayMetrics metrics = new DisplayMetrics();
        manager = this.getWindowManager();
        manager.getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        tabLength = width / 3;
        LayoutParams lp = slide_rod.getLayoutParams();
        lp.width = tabLength;
        slide_rod.setLayoutParams(lp);
    }

    public void initViewPage() {
        WaitReceive waitReceive = new WaitReceive();
        AlreadyReceive alreadyReceive = new AlreadyReceive();
        WaitPayment waitPayment = new WaitPayment();
        fragmentList.add(waitReceive);
        fragmentList.add(alreadyReceive);
        fragmentList.add(waitPayment);
        FragmentViewPagerAdapter adapter = new FragmentViewPagerAdapter(getSupportFragmentManager(), pager, fragmentList);
        adapter.setOnExtraPageChangeListener(new FragmentViewPagerAdapter.OnExtraPageChangeListener() {
            @Override
            public void onExtraPageScrolled(int i, float v, int i2) {
                RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) slide_rod
                        .getLayoutParams();
                if (currIndex == i) {
                    lp.leftMargin = (int) (currIndex * slide_rod.getWidth() + v * slide_rod.getWidth());
                } else if (currIndex > i) {
                    lp.leftMargin = (int) (currIndex * slide_rod.getWidth()
                            - (1 - v) * slide_rod.getWidth());
                }
                slide_rod.setLayoutParams(lp);
            }

            @Override
            public void onExtraPageSelected(int i) {
                switch (i) {
                    case 0:
                        tv_waitReceive.setTextColor(getResources().getColor(R.color.mainfontcolor_orange));
                        tv_waitPayment.setTextColor(getResources().getColor(R.color.text_unselect));
                        tv_alreadyReceive.setTextColor(getResources().getColor(R.color.text_unselect));
                        break;
                    case 1:
                        tv_waitReceive.setTextColor(getResources().getColor(R.color.text_unselect));
                        tv_waitPayment.setTextColor(getResources().getColor(R.color.text_unselect));
                        tv_alreadyReceive.setTextColor(getResources().getColor(R.color.mainfontcolor_orange));
                        break;
                    case 2:
                        tv_waitReceive.setTextColor(getResources().getColor(R.color.text_unselect));
                        tv_waitPayment.setTextColor(getResources().getColor(R.color.mainfontcolor_orange));
                        tv_alreadyReceive.setTextColor(getResources().getColor(R.color.text_unselect));
                        break;
                }
                currIndex = i;
            }

            @Override
            public void onExtraPageScrollStateChanged(int i) {

            }

        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wait_receive:
                pager.setCurrentItem(0);
                break;
            case R.id.already_receive:
                pager.setCurrentItem(1);
                break;
            case R.id.wait_payment:
                pager.setCurrentItem(2);
                break;
        }
    }
}
